package com.situ.edu.controller;

import com.situ.edu.pojo.entity.Elective;
import com.situ.edu.pojo.entity.Student;
import com.situ.edu.pojo.entity.Teacher;
import com.situ.edu.pojo.entity.Xi;
import com.situ.edu.pojo.query.StudentQuery;
import com.situ.edu.pojo.query.TeacherQuery;
import com.situ.edu.service.ITeacherStuService;
import com.situ.edu.service.IXiService;
import com.situ.edu.service.IXiStudentService;
import com.situ.edu.service.impl.TeacherStuServiceImpl;
import com.situ.edu.service.impl.XiServiceImpl;
import com.situ.edu.service.impl.XiStudentServiceImpl;
import com.situ.edu.util.MyBeanUtils;
import com.situ.edu.util.PageInfo;
import com.situ.edu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/teacherStu")
public class TeacherStuController extends BaseController{
    private ITeacherStuService teacherService = new TeacherStuServiceImpl();
    private IXiStudentService studentService = new XiStudentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method){
            case "selectByPage"://查询学生信息：请假
                selectByPage(req,resp);
                break;
            case "updateStatus"://是否准许学生请假
                updateStatus(req,resp);
                break;
            case "selectByStuId": //搜索学生id
                selectByStuId(req,resp);
                break;
            case "selectByTeaId": //搜索教师id
                selectByTeaId(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
        }
    }

    private void selectByTeaId(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("TeacherStuController.selectByTeaId");
        String userId = req.getParameter("id"); //获得userId
        Teacher teacher = teacherService.selectByTeaId(Integer.parseInt(userId));
        toJSON(resp,Result.ok(teacher));

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("TeacherStuController.update");
        Elective elective = MyBeanUtils.copyToBean(req,Elective.class);
        teacherService.update(elective);
        toJSON(resp,Result.ok("更新成功"));
    }

    //查询elective的
    private void selectByStuId(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("TeacherStuController.selectById");
        String id = req.getParameter("studentId");
        String courseId = req.getParameter("courseId");
        Elective elective = teacherService.selectByStuId(Integer.parseInt(id),Integer.parseInt(courseId));
        toJSON(resp,Result.ok(elective));
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiStudentController.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        studentService.updateStatus(Integer.parseInt(id),Integer.parseInt(status));
        toJSON(resp, Result.ok("更新成功"));
    }



    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("TeacherController.selectByPage");
        StudentQuery studentQuery = MyBeanUtils.copyToBean(req,StudentQuery.class);
        PageInfo pageInfo = teacherService.selectByPage(studentQuery);
        toJSON(resp,pageInfo);
    }


}
